<?php

namespace App\Http\Controllers\Wx;


use App\CodeResponse;
use App\Services\Goods\CatalogServices;
use Illuminate\Support\Facades\Request;

class CatalogController extends WxController
{
    protected $only = [];

    /**
     * 获取一级菜单
     * @param  Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $id = $request->input('id',0);
        $l1List = CatalogServices::getInstance()->getL1List();
        if(!$id){
            $current = $l1List->first();
        } else {
            $current = $l1List->where('id',$id)->first();
        }
        $l2List = null;
        if(!is_null($current)){
            $l2List = CatalogServices::getInstance()->getL2ListByPid($current->id);
        }
        return $this->success(
            [
                'categoryList' => $l1List,
                'currentCategory' => $current,
                'currentSubCategory' => $l2List,
            ]
        );
    }

    /**
     * 获取当前菜单
     * @param  Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function current(Request $request){
        $id = $request->input('id',0);
        if(!$id){
            return $this->fail(CodeResponse::PARAM_VALUE_ILLEGAL);
        }
        $category = CatalogServices::getInstance()->getL1ById($id);
        if(is_null($category)){
            return $this->fail(CodeResponse::PARAM_VALUE_ILLEGAL);
        }
        $l2List = CatalogServices::getInstance()->getL2ListByPid($category->id);
        return $this->success([
            'currentCategory' => $category,
            'currentSubCategory' => $l2List
        ]);
    }
}
